package com.hrc.springboot.service;

import com.hrc.springboot.bean.Department;
import com.hrc.springboot.mapper.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.stereotype.Service;

@CacheConfig(cacheNames = "dept"/*, cacheManager = "redisCacheManager"*/)
@Service
public class DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    @Qualifier("redisCacheManager")
    private RedisCacheManager redisCacheManager;


   /* @Cacheable
    public Department getDeptById(Integer id) {
        System.out.printf("已拿到%s号部门信息\n", id);
        return departmentMapper.getDeptById(id);
    }*/

    // 使用缓存管理器得到缓存，实现API调用
    public Department getDeptById(Integer id) {
        Department dept = departmentMapper.getDeptById(id);
        Cache cache = redisCacheManager.getCache("dept");
        cache.put("dept-02", dept);
        return dept;
    }

}
